/**
 * 阈值设置模拟数据
 * 提供阈值设置相关的模拟数据和方法
 */

const ThresholdSettingMockData = (function() {
    // 模拟阈值设置数据
    let thresholdSettings = [
        {
            id: 1,
            paramType: 'temperature',
            minValue: 5,
            maxValue: 35,
            unit: '℃',
            description: '粮食储存适宜温度范围',
            createTime: Date.now() - 86400000 * 30,
            updateTime: Date.now() - 86400000 * 10
        },
        {
            id: 2,
            paramType: 'humidity',
            minValue: 45,
            maxValue: 75,
            unit: '%',
            description: '粮食储存适宜湿度范围',
            createTime: Date.now() - 86400000 * 30,
            updateTime: Date.now() - 86400000 * 10
        },
        {
            id: 3,
            paramType: 'pest',
            minValue: 0,
            maxValue: 20,
            unit: '只/kg',
            description: '粮食虫害警戒阈值',
            createTime: Date.now() - 86400000 * 30,
            updateTime: Date.now() - 86400000 * 10
        },
        {
            id: 4,
            paramType: 'oxygen',
            minValue: 19,
            maxValue: 23,
            unit: '%',
            description: '安全氧气浓度范围',
            createTime: Date.now() - 86400000 * 30,
            updateTime: Date.now() - 86400000 * 10
        },
        {
            id: 5,
            paramType: 'co2',
            minValue: 0,
            maxValue: 0.5,
            unit: '%',
            description: '安全二氧化碳浓度范围',
            createTime: Date.now() - 86400000 * 30,
            updateTime: Date.now() - 86400000 * 10
        },
        {
            id: 6,
            paramType: 'ph3',
            minValue: 0,
            maxValue: 0.01,
            unit: 'mg/m³',
            description: '安全磷化氢浓度范围',
            createTime: Date.now() - 86400000 * 30,
            updateTime: Date.now() - 86400000 * 10
        }
    ];

    /**
     * 获取所有阈值设置
     * @returns {Promise} 包含所有阈值设置的Promise
     */
    function getThresholdSettings() {
        console.log('[Mock] 获取所有阈值设置');
        return new Promise((resolve) => {
            setTimeout(() => {
                resolve({
                    success: true,
                    code: 200,
                    message: '获取阈值设置成功',
                    data: JSON.parse(JSON.stringify(thresholdSettings))
                });
            }, 300);
        });
    }

    /**
     * 更新单个阈值设置
     * @param {Object} setting 阈值设置对象
     * @returns {Promise} 包含更新结果的Promise
     */
    function updateThresholdSetting(setting) {
        console.log('[Mock] 更新阈值设置:', setting);
        return new Promise((resolve) => {
            setTimeout(() => {
                const index = thresholdSettings.findIndex(s => s.paramType === setting.paramType);
                
                if (index >= 0) {
                    // 更新现有设置
                    thresholdSettings[index] = {
                        ...thresholdSettings[index],
                        minValue: setting.minValue,
                        maxValue: setting.maxValue,
                        updateTime: Date.now()
                    };
                    
                    resolve({
                        success: true,
                        code: 200,
                        message: '阈值范围更新成功',
                        data: JSON.parse(JSON.stringify(thresholdSettings[index]))
                    });
                } else {
                    // 创建新设置
                    const newSetting = {
                        id: thresholdSettings.length + 1,
                        paramType: setting.paramType,
                        minValue: setting.minValue,
                        maxValue: setting.maxValue,
                        unit: setting.unit || '',
                        description: setting.description || '',
                        createTime: Date.now(),
                        updateTime: Date.now()
                    };
                    
                    thresholdSettings.push(newSetting);
                    
                    resolve({
                        success: true,
                        code: 201,
                        message: '阈值设置创建成功',
                        data: JSON.parse(JSON.stringify(newSetting))
                    });
                }
            }, 500);
        });
    }

    // 导出公共方法
    return {
        getThresholdSettings,
        updateThresholdSetting
    };
})();

// 导出到全局作用域
window.ThresholdSettingMockData = ThresholdSettingMockData; 